﻿using AVCUMPlatform.BLL.Interface;
using AVCUMPlatform.BLL;
using Microsoft.AspNetCore.Mvc;
using AVCUMPlatform.Entities;
using AVCUMPlatform.Models;

namespace AVCUMPlatform.Controllers
{
    /// <summary>
    /// 客户端配置
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ClientInfoController : ControllerBase
    {
        private IClientInfoBLL _clientInfoBll = BLLContainer.Resolve<IClientInfoBLL>();
        private ISourceBLL _sourceBll = BLLContainer.Resolve<ISourceBLL>();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="field">排序字段</param>
        /// <param name="order">排序方式</param>
        /// <returns></returns>
        [HttpGet]
        [NoNeedLogin]
        public ActionResult GetClientInfoList(int page, int limit, string? field, string? order)
        {
            var clientlist = _clientInfoBll.GetAll();
            var list = from cl in clientlist
                       join s in _sourceBll.GetAll() on cl.Source equals s.Id into source
                       from sr in source.DefaultIfEmpty()
                       select new ClientInfoVM
                       {
                           Id = cl.Id,
                           ClientTypeStr = cl.ClientType == 1 ? "微信" : cl.ClientType == 2 ? "钉钉" : cl.ClientType == 3 ? "企业微信" : cl.ClientType == 4 ? "微信小程序": cl.ClientType == 5 ? "短信" : "",
                           ClientType = cl.ClientType,
                           Source = sr != null ? sr.Id : 0,
                           SourceStr = sr != null ? $"{sr?.Name}({sr?.Code})" : string.Empty,
                           AppId = cl.AppId,
                           AppSecret = cl.AppSecret,
                           ClientHost = cl.ClientHost,
                           CorpId = cl.CorpId,
                           AgendId = cl.AgendId,
                           Token = cl.Token,
                           AESKey = cl.AESKey
                       };
            if (!string.IsNullOrWhiteSpace(field) && field != "undefined")
            {
                if (!string.IsNullOrWhiteSpace(order) && order != "undefined")
                {
                    if (order == "desc")
                    {
                        if (field == "id")
                        {
                            list = list.OrderByDescending(x => x.Id).ToList();
                        }
                        else if (field == "clientTypeStr")
                        {
                            list = list.OrderByDescending(x => x.ClientType).ToList();
                        }
                    }
                    else
                    {
                        if (field == "id")
                        {
                            list = list.OrderBy(x => x.Id).ToList();
                        }
                        else if (field == "clientTypeStr")
                        {
                            list = list.OrderBy(x => x.ClientType).ToList();
                        }
                    }
                }
            }
            var count = list.Count();
            list = list.Skip((page - 1) * limit).Take(limit).ToList();
            var obj = new { code = 0, count, data = list, msg = "" };
            return new JsonResult(obj);
        }

        /// <summary>
        /// 获取客户端信息
        /// </summary>
        /// <param name="clientType"></param>
        /// <returns></returns>
        [HttpGet]
        [NoNeedLogin]
        public ActionResult GetClientInfo(int clientType)
        {
            var list = _clientInfoBll.GetList(out int totalcount, 1, 1000);
            if (clientType > 0)
            {
                list = list.Where(x=>x.ClientType == clientType).ToList();
                return new JsonResult(list);
            }
            else
            {
                return new JsonResult(list);
            }
        }
        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="clientInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [NoNeedLogin]
        public ResultModel AddOrUpdate(ClientInfo clientInfo)
        {
            var ret = new ResultModel();
            if (clientInfo.Id > 0)
            {
                ret.code = _clientInfoBll.Update(clientInfo) ? 1 : -1;
            }
            else
            {
                ret.code = _clientInfoBll.Add(clientInfo) ? 1 : -1;
            }
            ret.code = 1;
            return ret;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [NoNeedLogin]
        public ResultModel Delete(int id)
        {
            var ret = new ResultModel();
            ret.code = _clientInfoBll.Delete(new List<int>() { id }) ? 1 : -1;
            if (ret.code == -1)
            {
                ret.msg = "删除失败";
            }
            return ret;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        [NoNeedLogin]
        public ResultModel BatchDelete(string ids)
        {
            var ret = new ResultModel();
            var idList = ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            ret.code = _clientInfoBll.Delete(idList) ? 1 : -1;
            if (ret.code == -1)
            {
                ret.msg = "删除失败";
            }
            return ret;
        }


        /// <summary>
        /// 获取企业用户信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        //[HttpGet]
        //public ActionResult GetCorpWXUserInfo(string code)
        //{
        //    var userId = "";
        //    var model = ApiHelperFactory.GetCorpWXInstance().GetUserInfo(code);
        //    if (model != null)
        //    {
        //        userId = model.UserId;
        //    }
        //    return new JsonResult(userId);
        //}
    }
}
